package com.lotter.api.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lotter.api.service.BankCardService;
import com.lotter.api.service.SmsService;
import com.lotter.api.service.UserAccountService;
import com.lotter.common.context.YcContext;
import com.lotter.common.enumobject.BindCardOperateEnum;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.CommonUtil;
import com.lotter.common.util.MD5Util;
import com.lotter.common.util.StringUtil;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.bankcard.BankCard;
import com.lotter.model.user.UserAccount;


/**
 * 绑卡业务
 * @author zhangzechi
 *
 */
@Controller
@RequestMapping(value="/website/bankcard")
public class BankCardController extends BaseController {
	
	@Autowired
	private BankCardService bankCardService;
	
	@Autowired
	private UserAccountService userAccountService ;
	
	
	@Autowired
	private SmsService smsService;
	
	/**
	 * 绑卡
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/findBank",method=RequestMethod.GET)
	public Map<String,Object> findBank(HttpServletRequest request,
			@RequestParam(value ="token",required = true) String token) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Long uid = StaticDataContext.onLineUid(token);
			String mobile = StaticDataContext.onLineMobile(token);
			result.put("mobile",mobile);
			Boolean bool = bankCardService.findBankCardByUid(uid);
			if(bool){
				result.put("bindBack", 1);  //已绑卡
			}else{
				result.put("bindBack", 0);  //未绑卡
			}
		} catch (LotterCusException e) {
			logger.error("查询绑定银行卡出错,方法insert_Get 请检查代码");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess(result);
	}
	
	/**
	 * 绑卡
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public Map<String,Object> insert(HttpServletRequest request,
			@RequestParam(value ="token",required = true) String token) {
		String bankcard = getString("bankcard");
		if(StringUtil.isBlank(bankcard)){
			return ajaxLotterStage("银行名称不能为空");
		}
		
		String bankName = getString("bankName");
		if(StringUtil.isBlank(bankName)){
			return ajaxLotterStage("银行编号不能为空");
		}

		
		String realname = getString("realname");
		if(!CommonUtil.verAccountName(realname)){
			return ajaxLotterStage("真实姓名不正确");
		}
		String identitycards = getString("identitycards");
		if(StringUtil.isBlank(identitycards)){
			return ajaxLotterStage("身份证号码不能为空");
		}
		if (!identitycards.matches(YcContext.ID_PARTTEN))
			return ajaxLotterStage( "身份证号码不符合规则");
		
		String paypwd = getString("paypwd");
		if(StringUtil.isBlank(paypwd)){
			return ajaxLotterStage("支付密码不能为空");
		}
		
		if(paypwd.length() > 20 || paypwd.length() < 6){
			return ajaxLotterUser("密码长度不能小于6或者大于20");
		}
		
		try {
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			if(userAccount == null){
				return ajaxLotterStage( "未找到相关用户信息");
			}
			if(MD5Util.md5Hex(paypwd).equals(userAccount.getPassword())){
				return ajaxLotterUser("支付密码不能和登录登录相同");
			}
			Boolean bool = bankCardService.findBankCardByUid(uid);
			if(bool){
				return ajaxLotterUser("绑卡已经存在，请重新操作");
			}else{
				BankCard bankCard = new BankCard();
				bankCard.setUid(userAccount.getId());
				bankCard.setBankName(bankName);
				bankCard.setBankcard(bankcard);
				bankCard.setRealname(realname);
				bankCard.setIdentitycards(identitycards);
				bankCard.setStatus(BindCardOperateEnum.BIND.getCode());
				bankCard.setCreatedate(new Date());
				bankCard.setPaypwd(paypwd);
				bankCardService.insert(bankCard);
			}
		} catch (LotterCusException e1) {
			logger.error("绑卡查询失败，请检查代码");
			return ajaxLotterServer("服务器异常,请联系管理员");
		}
		
		return ajaxLotterSuccess();
	}

	
	/**
	 * 绑定新卡  页面指向
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/bindNewBank",method=RequestMethod.POST)
	public Map<String,Object> bindNewBank(HttpServletRequest request,
			@RequestParam(value="realname",required= true)String realname,
			@RequestParam(value="smsRandomCode",required=true) String smsRandomCode,
			@RequestParam(value="bankName",required= true)String bankName,
			@RequestParam(value="bankcard",required= true)String bankcard,
			@RequestParam(value ="token",required = true) String token) {
		System.out.println(realname.length());
		if(StringUtil.isBlank(realname))
			return ajaxLotterStage("真实姓名不能为空");
		
		if(StringUtil.isBlank(bankName)){
			return ajaxLotterStage("银行名称不能为空");
		}
		
		if(StringUtil.isBlank(bankcard))
			return ajaxLotterStage("银行卡不能为空");
		
		if(StringUtil.isBlank(smsRandomCode))
			return ajaxLotterStage("手机短信验证码");
		
		if(!CommonUtil.verAccountName(realname)){
			return ajaxLotterStage("真实姓名不正确");
		}
		try {
			String mobile = StaticDataContext.onLineMobile(token);
			Long uid = StaticDataContext.onLineUid(token);
			String code = smsService.findMobileCode(mobile);
			if(StringUtil.isBlank(code))
				return ajaxLotterUser("手机短信已经过期");
			
			if(!smsRandomCode.equals(code)){
				return ajaxLotterUser("手机短信验证码不正确");
			}
			
			BankCard bankCard = bankCardService.findBank(uid);
			if(bankCard ==null){
				return ajaxLotterUser("你还没有绑定过银行卡，请重新操作");
			}
			if(!realname.equals(bankCard.getRealname()))
				return ajaxLotterUser("两次真实姓名不一致,请重新再试");
			
			bankCardService.update(bankCard, realname, bankName, bankcard);
			
		} catch (LotterCusException e) {
			logger.error("查询绑定银行卡出错,方法bindNewBank 请检查代码");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess();	
	}
	
	
	
	/**
	 * 找回支付密码  转向
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/payPwdTurn",method = RequestMethod.GET)
	public Map<String,Object> payPwdTurn(@RequestParam(value ="token",required = true) String token){
		String mobile = StaticDataContext.onLineMobile(token);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("mobile", mobile);
		return ajaxLotterSuccess(result);
	}
	
	/**
	 * 更新支付密码
	 * @param request
	 * @param mobile
	 * @param password
	 * @param smsRandomCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updatePwd",method = RequestMethod.POST)
	public Map<String,Object> updatePwd(@RequestParam(value ="token",required = true) String token,
    		@RequestParam(value="payPwd",required = true) String payPwd,
    		@RequestParam(value="smsRandomCode",required=true) String smsRandomCode){
		
		if(StringUtil.isBlank(smsRandomCode))
			return ajaxLotterStage("验证码不能为空");
    	
		if(!StringUtil.isNumeric(smsRandomCode))
			return ajaxLotterStage("验证码必须都是数字");
		
		if(smsRandomCode.length() != 6)
			return ajaxLotterStage("验证码数字必须是6位");
		
    	if(StringUtil.isBlank(payPwd))
			return ajaxLotterStage("新密码不能为空");
    	
    	if(payPwd.length() < 6)
    		return ajaxLotterStage("新密码位数不能小于6位");
    	
		try {
			String mobile = StaticDataContext.onLineMobile(token);
			
			String code = smsService.findMobileCode(mobile);
			if(StringUtil.isBlank(code))
				return ajaxLotterUser("手机短信已经过期");
			
			if(!smsRandomCode.equals(code)){
				return ajaxLotterUser("手机短信验证码不正确");
			}
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			if(MD5Util.md5Hex(payPwd).equals(userAccount.getPassword())){
				return ajaxLotterUser("支付密码不能和登录登录相同");
			}
			BankCard bankCard = bankCardService.findBank(uid);
			if(bankCard ==null){
				return ajaxLotterUser("没有绑定银行卡");
			}
			bankCardService.updateParpwd(bankCard.getId(), payPwd);
		}catch(LotterCusException e){
			logger.error("找回支付密码业务查询失败，请检查代码");
			return ajaxLotterServer("服务器异常,请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	
}
